<template>
  <el-dialog width="600px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="80px"
      label-position="right"
      class="submit-form"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="submitForm.username"
          placeholder="请输入用户名"
          :readonly="submitForm.id!== ''"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNum">
        <el-input v-model="submitForm.phoneNum" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="submitForm.email" placeholder="请输入邮箱" />
      </el-form-item>
<!--      <el-form-item v-if="!submitForm.id||submitForm.id===''" label="密码" prop="password">
        <el-input v-model="submitForm.password" placeholder="请输入密码" type="password" show-password />
      </el-form-item>-->
      <el-form-item label="部门" prop="deptId">
        <el-cascader
          v-model="submitForm.deptId"
          :show-all-levels="false"
          style="width: 100%"
          placeholder="请选择部门"
          :options="deptOptions"
          :props="{ expandTrigger: 'hover', emitPath: false, value: 'id', label: 'deptName', checkStrictly: true}"
          clearable
        >
          <template slot-scope="{ node, data }">
            <span>{{ data.deptName }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template>
        </el-cascader>
      </el-form-item>
      <el-form-item label="角色" prop="roleIds">
        <el-select
          v-model="submitForm.roleIds"
          multiple
          style="width: 100%"
          placeholder="请选择角色"
        >
          <el-option
            v-for="item in roleOptions"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-switch
          v-model="submitForm.state"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="可用"
          inactive-text="禁用"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveUser, updateUser, selectUserById } from '@/api/system/user'
import { selectRoleList } from '@/api/system/role'
import { selectDeptTree } from '@/api/system/dept'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      roleOptions: [],
      deptOptions: [],
      submitForm: {
        id: '',
        username: '',
        password: '',
        phoneNum: '',
        email: '',
        deptId: '',
        roleIds: [],
        state: '1'
      },
      submitFormRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'change' }],
        password: [{ required: true, message: '请输入密码', trigger: 'change' }],
        phoneNum: [{ required: true, message: '请输入手机号码', trigger: 'change' }],
        deptId: [{ required: true, message: '请选择部门', trigger: 'change' }],
        state: [{ required: true, message: '请选择状态', trigger: 'change' }],
        roleIds: [{ required: true, message: '请选择角色', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      this.getRoleList()
      this.getDeptList()
      if (!id) {
        this.dialogTitle = '新增用户'
        return
      }
      this.dialogTitle = '修改用户'
      this.formLoading = true
      selectUserById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = saveUser
          if (this.submitForm.id) {
            request = updateUser
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    // 获取角色
    getRoleList() {
      selectRoleList().then(resp => {
        if (resp.success) {
          this.roleOptions = resp.data
        }
      })
    },
    // 获取机构
    getDeptList() {
      selectDeptTree({}).then(resp => {
        if (resp.success) {
          this.deptOptions = resp.data
        }
      })
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
